<template>
  <div class="resources-two" @mouseenter="isHover = true" @mouseleave="isHover = false">
    <div class="resources-two-wrapper">
      <div class="resources-two-content">
        <div class="resources-two-content-top">
          <ul v-if="modelValue?.links?.length" class="resources-two-content-links">
            <li v-for="(link, idx) in modelValue.links" :key="idx">
              <IconLink
                :iconType="link.iconType"
                :url="link.url"
                :text="link.text"
                :light="link.light"
                @handleClick="handleClick"
              />
            </li>
          </ul>
        </div>
        <ul
          v-if="modelValue?.socialLinks?.length"
          class="resources-two-content-bottom"
        >
          <li v-for="(link, idx) in modelValue.socialLinks" :key="idx">
            <IconLink
              :iconType="link.iconType"
              :size="link.iconSize"
              :text="link.text"
              :url="link.url"
              :light="link.light"
              @handleClick="handleClick"
            />
          </li>
        </ul>
      </div>
    </div>

    <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import IconLink from '@/common/IconLink/index.vue'
import Operate from '@/common/Operate/index.vue'
import Setting from './setting.vue'
import { randomString } from '@/utils/common'

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => ({})
  },
  isPreview: {
    type: Boolean,
    default: true
  },
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste', 'showLightboxSubscribe', 'showLightboxDonation', 'handleClick', 'clickHandler'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'ResourcesTwo',
    links: [
      {
        id: randomString(),
        text: '耀华国际教育学校',
        iconType: 'pdf',
        isExternal: false,
        url: 'http://baidu.com',
      }
    ],
    socialLinks: [
      {
        id: randomString(),
        text: '耀华国际教育学校',
        iconType: 'pdf',
        iconSize: 'sm',
        isExternal: false,
        url: 'http://baidu.com',
      }
    ]
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

handleInit()

const handleClick = (url: string) => {
  emit('handleClick', url)
}
</script>

<style lang="scss" scoped>
.resources-two {
  position: relative;
}
.resources-two-content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.resources-two-content-top {
  display: flex;
  align-items: center;
}

.resources-two-content-top > *:not(:first-child) {
  margin-left: 24px;

  @media (max-width: 767.98px) {
    margin-left: 0;
  }
}

.resources-two-content-links li:not(:first-child) {
  margin-top: 12px;

  @media (max-width: 767.98px) {
    margin-top: 4px;
  }
}

.resources-two-content-bottom {
  display: flex;
}

.resources-two-content-bottom:not(:first-child) {
  margin-top: 24px;

  @media (max-width: 767.98px) {
    margin-top: 16px;
  }
}

.resources-two-content-bottom li:not(:first-child) {
  margin-left: 24px;
}

.resources-two-content-bottom li ::v-deep .icon-link .icon-link__icon {
  width: 24px;
  height: 24px;
}
</style>